package Fishbowl_test;
import java.util.ArrayList;


public class FishB extends Fish {
	public FishB(int x, int y) {
		super(x, y);
	}

	static int minToReproduce, maxToDie;

	@Override
	public void simulate() {
		if(fishbowl.getCell(this.x, this.y) != -1) {
			this.isAlive = false;
			return;
		}
		
		ArrayList<Integer> A = Fish.fishbowl.getNeighborhood(this.x, this.y, 1);
		ArrayList<Integer> available_spaces = Fish.fishbowl.getNeighborhood(this.x, this.y, 0);
		ArrayList<Integer> B = Fish.fishbowl.getNeighborhood(this.x, this.y, -1);
		
		
		
		
		if(B.size() == 0 && available_spaces.size() > 0 && this.food == FishB.minToReproduce) {
				this.food = 0;
				int index = Fishbowl.gen.nextInt(available_spaces.size());
				// bug indice era 1, visto testRBPositive
				fishbowl.setCell(this.x + Fishbowl.directions[available_spaces.get(index)][0], 
						this.y + Fishbowl.directions[available_spaces.get(index)][1], -1);
				
				fishbowl.addNewFish(new FishB(this.x + Fishbowl.directions[available_spaces.get(index)][0],
						this.y + Fishbowl.directions[available_spaces.get(index)][1]));
		} else if(A.size() > 0) {
				this.starvation = 0;
				this.food++;
				int index = Fishbowl.gen.nextInt(A.size());
				fishbowl.setCell(this.x, this.y, 0);
				this.x += Fishbowl.directions[A.get(index)][0];
				this.y += Fishbowl.directions[A.get(index)][1];
				fishbowl.setCell(this.x, this.y , -1);
				
		} else if(available_spaces.size() > 0) {
				this.food = 0;
				this.starvation++;
				int index = Fishbowl.gen.nextInt(available_spaces.size());
				fishbowl.setCell(this.x, this.y, 0);
				this.x += Fishbowl.directions[available_spaces.get(index)][0];
				this.y += Fishbowl.directions[available_spaces.get(index)][1];
				fishbowl.setCell(this.x, this.y, -1);
				
				if(this.starvation == FishB.maxToDie) {
					this.isAlive = false;
					// testMLimit revealed this error
					fishbowl.setCell(this.x, this.y, 0);
				}
		} else {
				this.food = 0;
				this.starvation++;
				if(this.starvation == FishB.maxToDie) {
					this.isAlive = false;
					// testMLimit revealed this error
					fishbowl.setCell(this.x, this.y, 0);
				}
		}	
	}

	
}
